package com.school.fuyuan.business.controller;

import com.school.fuyuan.business.model.po.Privilege;
import com.school.fuyuan.business.service.PrivilegeService;
import com.school.fuyuan.common.annotation.Authenticate;
import com.school.fuyuan.common.message.RequestMsg;
import com.school.fuyuan.common.message.ResponseMsg;
import com.school.fuyuan.common.util.ParameterUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zrhong
 * @version 1.0
 * @date 2019/3/26 21:02
 * @description
 */
@RestController
@RequestMapping("pri")
public class PrivilegeController {
    private final PrivilegeService privilegeService;

    public PrivilegeController(PrivilegeService privilegeService) {
        this.privilegeService = privilegeService;
    }


    /**
     * 获取权限
     * @return
     */
    @PostMapping("/getPrivileges")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/pri/getPrivileges"})
    public ResponseMsg<List<Privilege>> getPrivileges() {
        List<Privilege> roles = privilegeService.getPrivileges();
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setData(roles);
        return responseMsg;
    }

    /**
     * 新增权限点
     * @return
     */
    @PostMapping("/addPrivilege")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/pri/addPrivilege"})
    public ResponseMsg addPrivilege(@RequestBody @Valid RequestMsg<String> requestMsg) {
        String privilegePath = requestMsg.getData();
        ParameterUtils.assertValue("权限路径", privilegePath);
        privilegeService.addPrivilege(privilegePath);
        ResponseMsg responseMsg = new ResponseMsg();
        return responseMsg;
    }


    /**
     * 修改权限点
     * @return
     */
    @PostMapping("/updatePrivilege")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/pri/updatePrivilege"})
    public ResponseMsg updatePrivilege(@RequestBody @Valid RequestMsg<Privilege> requestMsg) {
        Privilege privilege = requestMsg.getData();
        ParameterUtils.assertValue("权限修改项", privilege);
        privilegeService.updatePrivilege(privilege);
        ResponseMsg responseMsg = new ResponseMsg();
        return responseMsg;
    }
    /**
     * 删除权限点
     * @return
     */
    @PostMapping("/deletePrivilege")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/pri/deletePrivilege"})
    public ResponseMsg deletePrivilege(@RequestBody @Valid RequestMsg<String> requestMsg) {
        String privilegeId = requestMsg.getData();
        ParameterUtils.assertValue("权限Id", privilegeId);
        privilegeService.deletePrivilege(privilegeId);
        ResponseMsg responseMsg = new ResponseMsg();
        return responseMsg;
    }
}
